package com.sleepcamel.exporter.mappingGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.sleepcamel.rest.domain.JavaClass;
import com.sleepcamel.rest.domain.Path;
import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sleepcamel.rest.domain.method.RESTMethodType;
import com.sleepcamel.rest.domain.method.parameter.MethodParameter;

public abstract class BaseMappingGenerator {

	protected JavaClass javaClass;
	private RESTMethodType restMethodType;
	private List<MethodParameter> parameterList = new ArrayList<MethodParameter>();
	
	public BaseMappingGenerator(RESTMethodType restMethodType) {
		this.restMethodType = restMethodType;
	}

	public Collection<? extends RESTMethod> generate(){
		return Arrays.asList(generateRESTMethod());
	}

	protected RESTMethod generateRESTMethod() {
		RESTMethod restMethod = new RESTMethod();
		restMethod.setType(restMethodType);
		restMethod.setName(getRESTMethodName());
		restMethod.setPath(getRESTMethodPath());
		restMethod.setReturnType(getRESTMethodReturnType());
		restMethod.setMethodBody(getRESTMethodBody());
		restMethod.setParameters(getMethodParameters());
		return restMethod;
	}

	abstract protected String getRESTMethodBody();
	
	abstract protected void addMethodParameters();
	
	public List<MethodParameter> getMethodParameters(){
		return parameterList;
	}
	
	protected void addMethodParameter(MethodParameter parameter){
		parameterList.add(parameter);
	}

	protected JavaClass getRESTMethodReturnType(){
		return JavaClass.voidClass;
	}

	protected Path getRESTMethodPath(){
		return new Path();
	}
	
	private String getRESTMethodName(){
		return restMethodType.name().toLowerCase()+StringUtils.capitalize(getRESTMethodEntityName())+getRESTMethodSuffix();
	}
	
	protected String getRESTMethodSuffix(){
		return "";
	}
	
	protected String getRESTMethodEntityName(){
		return javaClass.getName();
	}

	public void setJavaClass(JavaClass javaClass){
		this.javaClass = javaClass;
	}

}
